/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Connect.ConnectionJDBC;
import DatabasePOJO.binhluan;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author Mr-Nguyen
 */
public class binhLuanDAO {
    public static ArrayList<binhluan> getBinhLuanAtSanPhamID(String SanPhamID){
        ArrayList<binhluan> arr  = new ArrayList<binhluan>();
        ConnectionJDBC cnn = new ConnectionJDBC();
        try {
            String sql = "select us.TenDangNhap,bl.BinhLuan from binhluan bl, user us where bl.masanpham =" + SanPhamID + " and bl.MaUser = us.Mauser";
            
            cnn.Connect();
            ResultSet rs = cnn.getTable(sql);
            while(rs.next())
            {
               binhluan bl = new binhluan();
               bl.setBinhLuan(rs.getString("BinhLuan"));
               bl.setMaUser(rs.getString("TenDangNhap"));
               arr.add(bl);
            }
        } catch (Exception e) {
        }finally{
            cnn.Close();
            return arr;
        }
    }
    
    public static void addBinhLuan(binhluan bl)
    {
        ConnectionJDBC cnn = new ConnectionJDBC();
        cnn.Connect();
        String sql = "insert into binhluan(Binhluan,MaUser,MaSanPham) values(";
        sql += " '" + bl.getBinhLuan() + "'," + bl.getMaUser() + "," + bl.getMaSanPham() + ")";
        cnn.update(sql);
        cnn.Close();
    }
}
